package org.microblog.service.impl;

import org.apache.ibatis.session.SqlSession;
import org.microblog.dao.CollectionMapper;
import org.microblog.dao.LikesMapper;
import org.microblog.entity.Blog;
import org.microblog.entity.Likes;
import org.microblog.service.LikesService;
import org.microblog.util.DatabaseUtil;
import org.microblog.util.MyBatisUtil;
import org.microblog.util.Page;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LikesServiceImpl implements LikesService {
    SqlSession sqlSession = MyBatisUtil.createSqlSession();
    LikesMapper likesMapper = sqlSession.getMapper(LikesMapper.class);
    CollectionMapper collectionMapper = sqlSession.getMapper(CollectionMapper.class);
    @Override
    public int getLikeCountByBid(int bid) throws SQLException {
        Connection conn = null;
        try{
            conn  = DatabaseUtil.getConnection();
            return likesMapper.getLikeCountByBid(bid);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public int deleteLikesByBid(int bid) throws SQLException {
        Connection conn = null;
        int result;
        try {
            result = likesMapper.deleteLikesByBid(bid);

            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            if (conn != null)
                try {
                    conn.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            throw e;
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

    @Override
    public void getLikeBlogsByPage(int uid, String keyword, Page pageObj) throws SQLException {
        Connection conn = null;
        try{
            int totalCount = likesMapper.getLikesCountByUidAndKeyword(uid,keyword);
            pageObj.setTotalCount(totalCount); // 设置总数量并计算总页数
            if (totalCount > 0) {
                // 对末页进行控制
                if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount())
                    pageObj.setCurrPageNo(pageObj.getTotalPageCount());
                // 进行分页查询
                List<Blog> blogList = likesMapper.getLikeBlogsByPage(uid,keyword,pageObj.getCurrPageNo(),pageObj.getPageSize());
                pageObj.setBlogsList(blogList);
            } else {
                pageObj.setCurrPageNo(0);
                pageObj.setBlogsList(new ArrayList<Blog>());
            }
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public int addLikes(Likes likes) throws SQLException {
        Connection conn = null;
        int result;
        try {

            if(likesMapper.findLikes(likes.getUid(),likes.getBid())==null){
                result = likesMapper.addLikes(likes);
            }else{
                result = -1;
            }
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            if (conn != null)
                try {
                    conn.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            throw e;
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

    @Override
    public Likes findLikes(int uid, int bid) throws SQLException {
        Connection conn = null;
        try{
            return likesMapper.findLikes(uid, bid);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public int deleteLikes(Likes likes) throws SQLException {
        Connection conn = null;
        int result;
        try {
            if(likesMapper.findLikes(likes.getUid(),likes.getBid())!=null){
                result = likesMapper.deleteLikes(likes);
            }else{
                result = -1;
            }
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            if (conn != null)
                try {
                    conn.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            throw e;
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }
}
